﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _07_LargestAreaOfEqualElements
{
    class Program
    {
        static public int NumberOfOccurences = 0;
        static public int BestNumberOfOccurences = 0;

        static void PrintMatrix(int[,] matrix)
        {
            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                for (int column = 0; column < matrix.GetLength(1); column++)
                {
                    Console.Write(matrix[row, column] + "\t");
                }
                Console.WriteLine();
            }
        }

        static bool IsInsideMatrix(int[,] matrix, int row, int column)
        {
            return row >= 0 && row < matrix.GetLength(0) && column >= 0 && column < matrix.GetLength(1);        
        }

        static void Bfs(int[,] searchedMatrix, int currentRow, int currentColumn, int number)
        {   
            searchedMatrix[currentRow, currentColumn] = 0;
            NumberOfOccurences++;

            if (IsInsideMatrix(searchedMatrix, currentRow - 1, currentColumn) &&
                searchedMatrix[currentRow - 1, currentColumn] == number)
            {
                Bfs(searchedMatrix, currentRow - 1, currentColumn, number);
            }

            if (IsInsideMatrix(searchedMatrix, currentRow + 1, currentColumn) &&
                searchedMatrix[currentRow + 1, currentColumn] == number
            )
            {
                Bfs(searchedMatrix, currentRow + 1, currentColumn, number);
            }

            if (IsInsideMatrix(searchedMatrix, currentRow, currentColumn - 1) &&
                searchedMatrix[currentRow, currentColumn - 1] == number)
            {
                Bfs(searchedMatrix, currentRow, currentColumn - 1, number);
            }

            if (IsInsideMatrix(searchedMatrix, currentRow, currentColumn + 1) &&
                searchedMatrix[currentRow, currentColumn + 1] == number)
            {
                Bfs(searchedMatrix, currentRow, currentColumn + 1, number);
            }
        }

        static void Main(string[] args)
        {
            int numberOfRows = int.Parse(Console.ReadLine());
            int numberOfColumns = int.Parse(Console.ReadLine());

            int[,] searchedMatrix = new int[numberOfRows, numberOfColumns];

            for (int row = 0; row < numberOfRows; row++)
            {
                for (int column = 0; column < numberOfColumns; column++)
                {
                    searchedMatrix[row, column] = int.Parse(Console.ReadLine());
                }
            }
            for (int row = 0; row < numberOfRows; row++)
            {
                for (int column = 0; column < numberOfColumns; column++)
                {
                    if (searchedMatrix[row, column] != 0)
                    {
                        Bfs(searchedMatrix, row, column, searchedMatrix[row, column]);
                        PrintMatrix(searchedMatrix);
                        Console.WriteLine(NumberOfOccurences);
                        if (NumberOfOccurences > BestNumberOfOccurences)
                        {
                            BestNumberOfOccurences = NumberOfOccurences;  
                        }
                        NumberOfOccurences = 0;
                        Console.WriteLine();
                    }
                }
            }

            Console.WriteLine(BestNumberOfOccurences);
        }
    }
}



//1
//3
//2
//2
//2
//4
//3
//3
//3
//2
//4
//4
//4
//3
//1
//2
//3
//3
//4
//3
//1
//3
//3
//1
//4
//3
//3
//3
//1
//1